/*	Author		: Arga
 *	Created On	: Apr 25, 2011
 *	Description	: 
 */

package team8.paacdroid.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import team8.paacdroid.R;
import team8.paacdroid.bean.BusRide;
import team8.paacdroid.bean.Itinerary;
import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ItineraryAdapter extends ArrayAdapter<Itinerary> {
	private ArrayList<Itinerary> items;

	public ItineraryAdapter(Context context, int textViewResourceId, ArrayList<Itinerary> items) {
		super(context, textViewResourceId, items);
		this.items = items;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		
		// Pop up the view
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.itinerary_item, null);
		}
		
		// Get the itinerary to display
		Itinerary i = items.get(position);
		if (i != null) {
			// Get the widgets
			TextView tvRouteNo = (TextView) v.findViewById(R.id.tvRouteNo);
			TextView tvRouteName = (TextView) v.findViewById(R.id.tvRouteName);
			TextView tvDirection = (TextView) v.findViewById(R.id.tvDirection);
			TextView tvBoardTime = (TextView) v.findViewById(R.id.tvBoardTime);
			TextView tvNumOfBusRides = (TextView) v.findViewById(R.id.tvNumOfBusRides);
			TextView tvEta = (TextView) v.findViewById(R.id.tvEta);
			
			// Get the data from the first busride
			if (i.getBusRides() != null && i.getBusRides().size()>0 && i.getBusRides().get(0) != null) {
				BusRide br = i.getBusRides().get(0);
				tvRouteNo.setText(br.getRouteNo() != null ? br.getRouteNo() : "");
				tvRouteName.setText(br.getRouteName() != null ? br.getRouteName() : "");
				tvDirection.setText(br.getDirection() != null ? br.getDirection() : "");
				
				// Get and display board time
				Date dtBoardTime = br.getBoardTime();
				if (dtBoardTime != null) {
					DateFormat dfTime = new SimpleDateFormat("h:mm aa");
					DateFormat dfDate = new SimpleDateFormat("M/d/yyyy");
					tvBoardTime.setText(dfTime.format(dtBoardTime));
					
					// While we're at it, calculate the ETA as well, but only if it's on today's date.
					Date now = new Date();
//					if (dfDate.format(now).equals(dfDate.format(dtBoardTime))) {
						String strEta = "";
//						Long minDiff = ((dtBoardTime.getTime() - now.getTime())/1000/60);
						
						// Display the eta
						tvEta.setText(strEta);
//					}
				}
				
				// Display number of bus rides
				String strNumOfBusRides = i.getNumOfBusRides() != null ? i.getNumOfBusRides().toString() + " bus ride" : "";
				if (i.getNumOfBusRides() != null && i.getNumOfBusRides() > 1) strNumOfBusRides += "s";
				tvNumOfBusRides.setText(strNumOfBusRides);
			}
			
		}

		// Return
		return v;
	}
}
